<?php

/**
 * Description of Machine
 *
 * @author markos
 */
class Machine {

    const TABLE_NAME = "maquina_virtual";

    private $log;
    private $dbAccess;

    public function __construct($log, $db) {
        $this->log = $log;
        $this->dbAccess = $db;
    }

    public function save($data, $code = null) {
        try {
            $this->log->info("[ code ] " . print_r($code, true));
            $this->log->info("[ data ] " . print_r($data, true));

            if (!is_null($code['value'])) {
                $this->dbAccess->getDb()->update(self::TABLE_NAME, $data, array("{$code['key']} = ?" => $code['value']));
                $data[$code['key']] = $code['value'];
            } else {
                $this->dbAccess->getDb()->insert(self::TABLE_NAME, $data);
                $data[$code['key']] = $this->dbAccess->getDb()->lastInsertId(self::TABLE_NAME);
            }
            return $data;
        } catch (Exception $e) {
            $this->log->err($e->getTraceAsString());
            throw $e;
        }
    }
}

?>
